<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 运营商控制器
 */


class YunyingController extends Controller{
    /**
     * 运营商列表
     */
    public function index(){
        $where['pid']=$_SESSION['yid'];
        $page=I('get.page','1','strip_tags');
        $count=M('yunying_user')->where($where)->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
        $result=M('yunying_user')->where($where)->limit($pagearr['start'],$pagearr['pagesize'])->select();
     foreach ($result as $key => $value) {
         if($value['rank']==2){
            $result[$key]['rank']='市代理';
            $str='';
            $str.=M('area')->where("region_id='%d'",$value['sheng_id'])->getField("region_name");
            $str.=M('area')->where("region_id='%d'",$value['shi_id'])->getField("region_name");
            $result[$key]['dress']=$str;

         }if($value['rank']==3){
            $result[$key]['rank']='县代理';
             $str='';
            $str.=M('area')->where("region_id='%d'",$value['sheng_id'])->getField("region_name");
            $str.=M('area')->where("region_id='%d'",$value['shi_id'])->getField("region_name");
            $str.=M('area')->where("region_id='%d'",$value['area_id'])->getField("region_name");
            $result[$key]['dress']=$str;
         }
     }
     
        $this->assign('yunyings',$result);
        $this->assign('pagearr',$pagearr);
        $this->display();

    }
    /**
     * 添加运营商静态页面
     */
    public function add(){
        ///获取当前运营商的等级
        $rank=$_SESSION['rank'];
      
        $where['a.yid']=$_SESSION['yid'];
        if($rank==1){
            ///省级代理允许添加市级县级
            $sheng=M('yunying_user a')->join('cart_area b on a.sheng_id=b.region_id')->where($where)->getField("b.region_id,b.region_name");
            $shi=M('area')->where("parent_id='%d'",array_keys($sheng))->getField("region_id,region_name");
           
        }
        if($rank==2){
            ///市级代理允许添加县级代理
            $sheng=M('yunying_user a')->join('cart_area b on a.sheng_id=b.region_id')->where($where)->getField("b.region_id,b.region_name");
          $city=M('yunying_user a')->join('cart_area b on a.shi_id=b.region_id')->where($where)->getField("b.region_id,parent_id,b.region_name");
         foreach ($city as $k=>$v){
            $area=M('area')->where("parent_id='%d'",$v['region_id'])->getField("region_id,region_name");////获取该市区下的区域
         }
        }
        $this->assign('sheng',$sheng)->assign('shi',$shi)->assign('city',$city)->assign('area',$area);
      $this->assign('rank',$rank);
     

        $this->display();

    }
    /**
     * 保存添加的运营商
     */
    public function Saveadd(){
       /// var_dump($_POST);
        if($_SESSION['rank']==1){
            ///省级代理添加市级或县级
            $yun=M('yunying_user');
            $yun->create();
            $yun->yname=I('post.yname','','strip_tags');
            $yun->companyname=I('post.companyname','','strip_tags');
            $yun->connectname=I('post.connectname','','strip_tags');
            $yun->mobile=I('post.mobile','','strip_tags');
            $yun->qq=I('post.qq','','strip_tags');
            $yun->pid=$_SESSION['yid'];///获取该运营商属于哪个运营商
            $yun->sheng_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('sheng_id');///获取省
            $yun->shi_id=I('post.shi_id','','strip_tags');///获取市
           $area_id=I('post.area_id','','strip_tags');
            $yun->area_id=$area_id;//获取区域
            if(empty($area_id)){
                $yun->rank='2';///添加的是市级代理
            }else{
                $yun->rank='3';///添加的为县级代理
            }
            $yun->password=!empty($_POST['pass'])?md5($_POST['pass']):'';
            $result=$yun->add();
            if($result>0){
              echo"<script>alert('代理商添加成功');location.href='/index.php/Admin/Yunying/index'</script>";
           
            }else{
               echo"<script>alert('代理商添加失败');location.href='/index.php/Admin/Yunying/add'</script>";

            }
        }
         if($_SESSION['rank']==2){
            ///市级运营商添加县运营商
            $yun=M('yunying_user');
            $yun->create();
            $yun->yname=I('post.yname','','strip_tags');
            $yun->companyname=I('post.companyname','','strip_tags');
            $yun->connectname=I('post.connectname','','strip_tags');
            $yun->mobile=I('post.mobile','','strip_tags');
            $yun->qq=I('post.qq','','strip_tags');
            $yun->pid=$_SESSION['yid'];///获取该运营商属于哪个运营商
            $yun->sheng_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('sheng_id');///获取省
            $yun->shi_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('shi_id');;///获取市
            $yun->area_id=I('post.area_id','','strip_tags');//获取区域
            $yun->password=!empty($_POST['pass'])?md5($_POST['pass']):'';
            $yun->rank='3';
            $res=$yun->add();
            if($res>0){
               echo"<script>alert('代理商添加成功');location.href='/index.php/Admin/Yunying/index'</script>";

            }else{
              echo"<script>alert('代理商添加失败');location.href='/index.php/Admin/Yunying/add'</script>";


            }
           

         }

    }
    /**
     * 运营商编辑
     */
    public function edit(){
  $rank=$_SESSION['rank'];///获取登陆运营商的等级

 $where['yid']=$_GET['yid'];
 $result=M('yunying_user')->where($where)->find();
    $sheng=M('area')->where("region_id='%d'",$result['sheng_id'])->getField("region_id,region_name");
    $city=M('area')->where("region_id='%d'",$result['shi_id'])->getField("region_id,region_name");
    $area=M('area')->where("region_id='%d'",$result['area_id'])->getField("region_id,region_name");
    $cityall=M('area')->where("parent_id='%d'",$result['sheng_id'])->getField("region_id,region_name");///获取该省下的所有区域

    $areaall=M('area')->where("parent_id='%d'",$result['shi_id'])->getField("region_id,region_name");///获取该市下的所有区域
    $this->assign('all',$areaall)->assign('cityall',$cityall);
 $this->assign('sheng',$sheng)
 ->assign('city',$city)->assign('area',$area)
 ->assign('yun',$result)->assign('rank',$rank)
->assign('result',$result);
 $this->display();
    }
    /**
     * 保存编辑
     */
    public function Saveedit(){
      $yun=M('yunying_user');
          $yun->yname=I('post.yname','','strip_tags');
            $yun->companyname=I('post.companyname','','strip_tags');
            $yun->connectname=I('post.connectname','','strip_tags');
            $yun->mobile=I('post.mobile','','strip_tags');
            $yun->qq=I('post.qq','','strip_tags');
            $yun->pid=$_SESSION['yid'];///获取该运营商属于哪个运营商
            if($_SESSION['rank']==1){
              ////如果父级为省级运营商
              $yun->sheng_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('sheng_id');///获取省
              $yun->shi_id=I('post.shi_id',0,'strip_tags');///获取市
              $area_id=I('post.area_id',0,'strip_tags');///获取县
              $yun->area_id=$area_id;
            
              if(!empty($area_id)){

                $yun->rank='3';///县级代理
              }else{
                $yun->rank='2';///市级代理
              }

            }if($_SESSION['rank']==2){
              ///父级为市级代理
               $yun->sheng_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('sheng_id');///获取省
              $yun->shi_id=M('yunying_user')->where("yid='%d'",$_SESSION['yid'])->getField('shi_id');///获取市
              $yun->area_id=I('post.area_id',0,'strip_tags');///获取县
              $yun->rank='3';///县级代理

            }
             $pass=I('post.pass','','strip_tags');

       if (!empty($pass)) 
       {
        $yun->password=md5($pass);
       }
       $re=$yun->where("yid='%d'",$_GET['yid'])->save();
       //echo M('')->getLastSql();die;
       
      if($re!==false){
        echo "<script>alert('运营商修改成功');location.href='/index.php/Admin/Yunying/index'</script>";

      }else{
        echo "<script>alert('运营商编辑错误');location.href='/index.php/Admin/yunying/edit'</script>";

      }
           
            
           


    }
   /**
     *三级联动获取区域
     */
    public function area(){
        $where['parent_id']=I('get.region','','strip_tags');
        $result=M('area')->where($where)->getField("region_id,region_name");
        $str='';
        foreach($result as $k=>$v){
          $str.= '<option value='.$k.'>'.$v.'</option>';

        }
        echo $str;

    }

}